{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']     # 显示中文\n",
    "# 为了坐标轴负号正常显示。matplotlib默认不支持中文，设置中文字体后，负号会显示异常。需要手动将坐标轴负号设为False才能正常显示负号。\n",
    "plt.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "   Unnamed: 0      UserID  EI  NS  TF  JP MBTI类型  粉丝数  关注数  性别  ...  烦恼  玩  \\\n0           0  1330417035   1   1   1   0   ISFJ   71  276   0  ...   0  1   \n\n   人生  哈哈哈  下午  可能  严重  奶茶  小女孩  急急  \n0   0    1   0   0   0   0    0   1  \n\n[1 rows x 513 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Unnamed: 0</th>\n      <th>UserID</th>\n      <th>EI</th>\n      <th>NS</th>\n      <th>TF</th>\n      <th>JP</th>\n      <th>MBTI类型</th>\n      <th>粉丝数</th>\n      <th>关注数</th>\n      <th>性别</th>\n      <th>...</th>\n      <th>烦恼</th>\n      <th>玩</th>\n      <th>人生</th>\n      <th>哈哈哈</th>\n      <th>下午</th>\n      <th>可能</th>\n      <th>严重</th>\n      <th>奶茶</th>\n      <th>小女孩</th>\n      <th>急急</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>1330417035</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>ISFJ</td>\n      <td>71</td>\n      <td>276</td>\n      <td>0</td>\n      <td>...</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n<p>1 rows × 513 columns</p>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_data = pd.read_csv('../data/mbti_weibo_data_cantrain.csv')\n",
    "all_data.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "   Unnamed: 0      UserID  EI  NS  TF  JP MBTI类型  粉丝数  关注数  性别  ...  贴  感到  \\\n0           0  1330417035   1   1   1   0   ISFJ   71  276   0  ...  0   0   \n\n   时刻  一份  吃  回复  个人  喜欢  快来  干净  \n0   0   0  0   0   0   0   1   0  \n\n[1 rows x 2002 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Unnamed: 0</th>\n      <th>UserID</th>\n      <th>EI</th>\n      <th>NS</th>\n      <th>TF</th>\n      <th>JP</th>\n      <th>MBTI类型</th>\n      <th>粉丝数</th>\n      <th>关注数</th>\n      <th>性别</th>\n      <th>...</th>\n      <th>贴</th>\n      <th>感到</th>\n      <th>时刻</th>\n      <th>一份</th>\n      <th>吃</th>\n      <th>回复</th>\n      <th>个人</th>\n      <th>喜欢</th>\n      <th>快来</th>\n      <th>干净</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>1330417035</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>ISFJ</td>\n      <td>71</td>\n      <td>276</td>\n      <td>0</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>1 rows × 2002 columns</p>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_data = pd.read_csv('../data/mbti_weibo_data_cantrain1500.csv')\n",
    "all_data.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "       UserID  EI  NS  TF  JP MBTI类型\n0  1330417035   1   1   1   0   ISFJ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>UserID</th>\n      <th>EI</th>\n      <th>NS</th>\n      <th>TF</th>\n      <th>JP</th>\n      <th>MBTI类型</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1330417035</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>ISFJ</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_data = all_data.iloc[:,1:7]\n",
    "target_data.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "   粉丝数  关注数  性别  微博数  注册年限  互动数  视频累积播放量  TOP1  TOP2  TOP3  ...  贴  感到  时刻  \\\n0   71  276   0  655    11  109        0    29    20    14  ...  0   0   0   \n\n   一份  吃  回复  个人  喜欢  快来  干净  \n0   0  0   0   0   0   1   0  \n\n[1 rows x 1995 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>粉丝数</th>\n      <th>关注数</th>\n      <th>性别</th>\n      <th>微博数</th>\n      <th>注册年限</th>\n      <th>互动数</th>\n      <th>视频累积播放量</th>\n      <th>TOP1</th>\n      <th>TOP2</th>\n      <th>TOP3</th>\n      <th>...</th>\n      <th>贴</th>\n      <th>感到</th>\n      <th>时刻</th>\n      <th>一份</th>\n      <th>吃</th>\n      <th>回复</th>\n      <th>个人</th>\n      <th>喜欢</th>\n      <th>快来</th>\n      <th>干净</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>71</td>\n      <td>276</td>\n      <td>0</td>\n      <td>655</td>\n      <td>11</td>\n      <td>109</td>\n      <td>0</td>\n      <td>29</td>\n      <td>20</td>\n      <td>14</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>1 rows × 1995 columns</p>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features_data0 = all_data.iloc[:,7:2002]\n",
    "features_data0.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "   粉丝数  关注数  性别  微博数  注册年限  互动数  视频累积播放量  TOP1  TOP2  TOP3  TOP4  TOP5\n0   71  276   0  655    11  109        0    29    20    14     6     5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>粉丝数</th>\n      <th>关注数</th>\n      <th>性别</th>\n      <th>微博数</th>\n      <th>注册年限</th>\n      <th>互动数</th>\n      <th>视频累积播放量</th>\n      <th>TOP1</th>\n      <th>TOP2</th>\n      <th>TOP3</th>\n      <th>TOP4</th>\n      <th>TOP5</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>71</td>\n      <td>276</td>\n      <td>0</td>\n      <td>655</td>\n      <td>11</td>\n      <td>109</td>\n      <td>0</td>\n      <td>29</td>\n      <td>20</td>\n      <td>14</td>\n      <td>6</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features_data1 = all_data.iloc[:,7:19]\n",
    "features_data1.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "   readability1  readability2  readability3  乐_num  好_num  怒_num  哀_num  \\\n0     15.309524      0.738095       8.02381      8     25      0      5   \n\n   惧_num  恶_num  惊_num  ...  yg  rr  mq  rg   a  bg  p  x  b  j  \n0      0      7      0  ...   0   0   0   0  31   0  6  1  2  0  \n\n[1 rows x 71 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>readability1</th>\n      <th>readability2</th>\n      <th>readability3</th>\n      <th>乐_num</th>\n      <th>好_num</th>\n      <th>怒_num</th>\n      <th>哀_num</th>\n      <th>惧_num</th>\n      <th>恶_num</th>\n      <th>惊_num</th>\n      <th>...</th>\n      <th>yg</th>\n      <th>rr</th>\n      <th>mq</th>\n      <th>rg</th>\n      <th>a</th>\n      <th>bg</th>\n      <th>p</th>\n      <th>x</th>\n      <th>b</th>\n      <th>j</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>15.309524</td>\n      <td>0.738095</td>\n      <td>8.02381</td>\n      <td>8</td>\n      <td>25</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n      <td>7</td>\n      <td>0</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>31</td>\n      <td>0</td>\n      <td>6</td>\n      <td>1</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>1 rows × 71 columns</p>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features_data2 = all_data.iloc[:,54:125]\n",
    "features_data2.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "outputs": [
    {
     "data": {
      "text/plain": "   乐_num  好_num  怒_num  哀_num  惧_num  恶_num  惊_num  stopword_num  word_num  \\\n0      8     25      0      5      0      7      0           138       393   \n\n   sentence_num  all_num  \n0             1       45  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>乐_num</th>\n      <th>好_num</th>\n      <th>怒_num</th>\n      <th>哀_num</th>\n      <th>惧_num</th>\n      <th>恶_num</th>\n      <th>惊_num</th>\n      <th>stopword_num</th>\n      <th>word_num</th>\n      <th>sentence_num</th>\n      <th>all_num</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>8</td>\n      <td>25</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n      <td>7</td>\n      <td>0</td>\n      <td>138</td>\n      <td>393</td>\n      <td>1</td>\n      <td>45</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 243,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features_data= all_data[['乐_num','好_num','怒_num','哀_num','惧_num','恶_num','惊_num','stopword_num','word_num','sentence_num','all_num']]\n",
    "features_data.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# import modules\n",
    "from sklearn.feature_selection import (SelectKBest, chi2, SelectPercentile, SelectFromModel, SequentialFeatureSelector, SequentialFeatureSelector)\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# scaler = StandardScaler()\n",
    "\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "scaler = MinMaxScaler()\n",
    "\n",
    "y_EI = target_data[\"EI\"]\n",
    "y_NS = target_data[\"NS\"]\n",
    "y_TF = target_data[\"TF\"]\n",
    "y_JP = target_data[\"JP\"]\n",
    "\n",
    "y_list = {\"y_EI\":y_EI,\"y_NS\":y_NS,\"y_TF\":y_TF,\"y_JP\":y_JP}\n",
    "X  = features_data0\n",
    "\n",
    "# y = y_JP\n",
    "\n",
    "# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "def trainModelTest(X,y):\n",
    "    # y = Y\n",
    "    X  = scaler.fit_transform(X)  #对自变量X做标准化处理\n",
    "    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)\n",
    "\n",
    "    # # print(Ytest)\n",
    "    # clf = DecisionTreeClassifier(random_state=1)\n",
    "    # rfc = RandomForestClassifier(n_estimators=20, max_depth=4)\n",
    "    #\n",
    "    # clf = clf.fit(X_train,y_train)\n",
    "    # score_c = clf.score(X_test,y_test)\n",
    "    #\n",
    "    # rfc = rfc.fit(X_train,y_train)\n",
    "    # score_r = rfc.score(X_test,y_test)\n",
    "\n",
    "    # 创建逻辑回归模型\n",
    "    log = LogisticRegression()\n",
    "    log.fit(X_train, y_train)\n",
    "    score_l = log.score(X_test,y_test)\n",
    "\n",
    "    # print('Single Tree:{}'.format(score_c),'Random Forest:{}'.format(score_r),'LogisticRegression:{}'.format(score_l))\n",
    "    return score_l"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
